package com.kuang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.pojo.Books;
import com.kuang.service.BookService;
import com.kuang.service.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import java.awt.print.Book;
import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
     //controller调用service
    @Autowired
    @Qualifier("bookservice")
    private BookService bookService;
  //查询全部的书籍  并且返回到一个书籍展示的页面
    @RequestMapping("/allbook")
    public String list(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum, @RequestParam(required=true,value="pageSize",defaultValue="6")Integer pageSize,Model model){
      PageHelper.startPage(pageNum,pageSize);
        List<Books> list = bookService.quertAllBook();
        for (Books books : list) {
            System.out.println(books);
        }

        PageInfo<Books> pageInfo = new PageInfo<>(list);
        if (pageNum==0||pageNum>pageInfo.getPages()){
            return "redirect:/book/allbook";
        }
        model.addAttribute("list",pageInfo);

        return  "allBook";
  }
//跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPage(){
        return "addBook";
   }
//添加书籍的请求：
@RequestMapping("/addBook")
public String addBook(Books books){
    System.out.println("addBook="+books);
        bookService.addBook(books);
//重定向 到@RequestMapping("/allbook")  这个请求
        return "redirect:/book/allbook";
}
//跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toUpdatePaper(int id, Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBooks",books);

        return "updateBook";
    }

//修改书籍
    @RequestMapping("/updatebook")
    public String updateBook(Books books){
        System.out.println("updateBook=="+books);
        bookService.updateBook(books);
        return "redirect:/book/allbook";
    }
    //删除书籍
    @RequestMapping("/deleteBook/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allbook";
}
//查询
    @RequestMapping("/querybook")
 public String queryBook(String queryBookName,Model model){
        List<Books> list = bookService.queryBookByname(queryBookName);
      if (list==null){
          list=bookService.quertAllBook();
          model.addAttribute("error","未查到信息");
      }

        model.addAttribute("list",list);

        return  "allBook";

 }
    @RequestMapping("/finall")
    public String finall(@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum
            , @RequestParam(required=true,value="pageSize",defaultValue="10") Integer pageSize, Model model){


        PageHelper.startPage(pageNum,pageSize);

        List<Books> books = bookService.quertAllBook();
        PageInfo<Books> pageInfo = new PageInfo<>(books);
        if (pageNum==0 ||pageNum>pageInfo.getPages()){

            return "redirect:/book/finall";

        }



        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo.getPageNum());

        System.out.println(pageInfo.getSize());
        System.out.println();
        return  "main";

    }
    //转到供应商界面
    @RequestMapping("/supplier")
    public String redictSupplier(){
        return "supplier";
    }


}
